import Connector from "./connector";
import Logger from './logger';
import GEvent from './gevent'
import { throws } from "assert";
export default class Connectable {
    protected opts: {} = {};

    public logger;

    protected connector: Connector;

    protected event: any;

    protected notifyMap: {} = {};

    protected host: string;

    protected port: string;

    protected ssl: string;

    constructor(host: string, port: string, opts: any) {
        opts = opts || {};
        opts.event = opts.event || GEvent;
        opts.logger = opts.logger || Logger.sdk;
        this.opts = opts;
        this.logger = opts.logger;
        this.event = opts.event;
        this.notifyMap = {};
        this.host = host;
        this.port = port;
        this.ssl = opts.ssl;
        // this.connect();
    }

    connect() {
        this.connector = new Connector(this, this.host, this.port, this.opts);
    }

    request(route, message) {
        return this.connector.request(route, message)
    }

    notify(route, message) {
        this.connector.notify(route, message)
    }

    close() {
        try {
            this.connector.close();
        } catch (e) { }

        this.connector = null;
    }

    bindEvent(event, fn) {
        if (this.event) {
            this.event.onSingle(event, fn);
        } else {
            throw 'event object is not defined'
        }
    }

    unbindEvent() {

    }

    doWsError(event) {

    }

    /**
    * 
    * @param event 处理通知 ;  
    * 1、Team满了通知创建房间
    * 2、更新Team的组队情况
    */
    doNotify(event) {
        var fun = this.notifyMap[event.route];
        if (fun) {
            fun(event.body);
        }
    }
}